import { EntityTypeEnum, Singleton, StateTypeEnum } from "../Common";
import { LocalStorageTypeEnum } from "../Enum";
import DataManager from "./DataManager";

export default class BagManager extends Singleton {

    _bagData: { [id: number]: number } = null

    static get Ins() {
        return super.GetInstance<BagManager>()
    }

    get bagData() {
        if (!this._bagData) {
            const str = localStorage.getItem(LocalStorageTypeEnum.bagData) || "{}"
            const data = JSON.parse(str)
            this._bagData = data
        }
        return this._bagData
    }

    /** 获取道具数量 */
    getNumberById(id: number | string) {

        return this.bagData[id] || 0
    }

    /** 保存游戏结束数据 */
    saveGameOverData() {
        if (DataManager.Ins.state.state == StateTypeEnum.win) {
            this.bagData[EntityTypeEnum.scroll] = DataManager.Ins.state.scrollNum + this.bagData[EntityTypeEnum.scroll] || 0
        }
        const data = JSON.stringify(this.bagData)
        localStorage.setItem(LocalStorageTypeEnum.bagData, data)
    }

    /** 存储数据 */
    saveData() {
        const data = JSON.stringify(this.bagData)
        localStorage.setItem(LocalStorageTypeEnum.bagData, data)
    }

    /** 获得科技卷 */
    obtainScroll(num: number) {
        if (!this.bagData[EntityTypeEnum.scroll]) this.bagData[EntityTypeEnum.scroll] = 0
        this.bagData[EntityTypeEnum.scroll] += num
        const data = JSON.stringify(this.bagData)
        localStorage.setItem(LocalStorageTypeEnum.bagData, data)
    }

}